#include "rewardOrb.h"
#include "GameApp.h"

//-------------------------------------------------------------------------------------
rewardOrb::rewardOrb(const RewardType type, const float amount) : BaseObject("level/orb.xml")
{
	mType = type;
	mAmount = amount;
	mTypes.push_front(OBJECT_REWARDORB);
}
//-------------------------------------------------------------------------------------
rewardOrb::rewardOrb(boost::shared_ptr<rewardOrb> ref)
	 : BaseObject(ref)
{
	mType = ref->getRewardType();
	mAmount = ref->getAmount();
	mTypes.push_front(OBJECT_REWARDORB);
}
//-------------------------------------------------------------------------------------
rewardOrb::~rewardOrb(void)
{
}
//-------------------------------------------------------------------------------------
void rewardOrb::setRewardType(const RewardType type)
{
	mType = type;
}
//-------------------------------------------------------------------------------------
const RewardType rewardOrb::getRewardType(void) const
{
	return mType;
}
//-------------------------------------------------------------------------------------
void rewardOrb::setAmount(const float amount)
{
	mAmount = amount;
}
//-------------------------------------------------------------------------------------
const float rewardOrb::getAmount(void) const
{
	return mAmount;
}
//-------------------------------------------------------------------------------------
void rewardOrb::update(const unsigned long elapsedTime)
{
	btVector3 posPlayer = gpApp->mpGameLogic->getCharacter()->getBody()->getWorldTransform().getOrigin();
	btVector3 posOrb = mpBody->getWorldTransform().getOrigin();
	btScalar distanceToOrb = posPlayer.distance(posOrb);

	btScalar orbRange = 10.f;
	btScalar flyingForce = 1000.f;

	// only fly towards player when close
	if( distanceToOrb < orbRange )
	{
		btVector3 dir = posPlayer - posOrb;
		dir.normalize();
		// fly faster when close to orb 
		btVector3 theForce = (flyingForce / (distanceToOrb + 1.0f)) * dir * (elapsedTime / 1000.f);
		
		//mpBody->applyCentralForce(theForce);
		mpBody->setLinearVelocity(theForce);
		mpBody->activate();
	}
	else kill();
}